package com.yuyou.fn.timer.task;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.toolkit.IdWorker;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import com.xxl.job.core.log.XxlJobLogger;
import com.yuyou.fn.common.constant.ClassLevelEnum;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.service.ISiteConfigService;
import com.yuyou.fn.common.util.*;
import com.yuyou.fn.educational.entity.*;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.educational.vo.ExternalRegDetailImportVo;
import com.yuyou.fn.educational.vo.TmsRegMessage;
import com.yuyou.fn.platform.entity.AreaTeam;
import com.yuyou.fn.platform.service.IAreaTeamService;
import com.yuyou.fn.timer.vo.TmsQueryDetailVo;
import com.yuyou.fn.timer.vo.TmsQueryVo;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by Administrator on 2018/4/9.
 */
@JobHander(value = "AddExternalRegTask")
@Component
public class AddExternalRegTask extends IJobHandler {
    @Resource
    private IExternalRegDetailService externalRegDetailService;


    @Resource
    private IBaseGradeService baseGradeService;

    @Resource
    private IAreaTeamService areaTeamService;

    @Resource
    private IPeriodService periodService;


    @Resource
    private ISiteConfigService siteConfigService;


    @Override
    public ReturnT<String> execute(String... params) throws Exception {
        String paramQueryStudentReg = siteConfigService.findConfigValue("param_query_student_reg");

        JSONArray jsonArray = JSONArray.parseArray(paramQueryStudentReg);

        String token=externalRegDetailService.getZyToken();

        try {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String state = jsonObject.getString("state");
                if (!"1".equals(state)) {
                    continue;
                }
                String areaTeamName = jsonObject.getString("areaTeamName");
                if (StringUtils.isEmpty(areaTeamName)) {
                    return ReturnT.FAIL;
                }
                String[] periodNames = jsonObject.getString("periodNames").split(",");
                String yearName = jsonObject.getString("yearName");
                String[] seasonNames = jsonObject.getString("seasonNames").split(",");
                String[] gradeNames = jsonObject.getString("gradeNames").split(",");
                AreaTeam areaTeam = areaTeamService.findAreaTeamByName(areaTeamName);
                List<Period> periodList = periodService.findByAreaTeamIdsAndNames(Arrays.asList(areaTeam.getAreaTeamId()), Arrays.asList(periodNames));
                List<BaseGrade> baseGradeList = baseGradeService.findGradeByGradeNames(Arrays.asList(gradeNames));
                externalRegDetailService.dealZyExternalReg(areaTeam, periodList, baseGradeList, yearName, Arrays.asList(seasonNames), Arrays.asList(gradeNames),token);

            }
        } catch (Exception e) {
            XxlJobLogger.log("同步报读数据失败",e);
            return ReturnT.FAIL;
        }

        return ReturnT.SUCCESS;
    }

}
